package Leetcode第一期;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : K k
 * @date : 19:25 2020/9/8
 */
public class 顺时针打印矩阵_29 {
    public static int[] spiralOrder(int[][] matrix) {
        List<Integer> res=new ArrayList<>();
        if (matrix.length==0 || matrix[0].length==0){
            return new int[]{};
        }

        int c1=0,c2=matrix[0].length-1;
        int r1=0,r2=matrix.length-1;
        int[] ans=new int[(c2+1)*(r2+1)];
        while (r1<=r2 && c1<=c2){
            for (int c=c1;c<=c2;c++){
                res.add(matrix[r1][c]);
            }
            for (int r=r1+1;r<=r2;r++){
                res.add(matrix[r][c2]);
            }
            if (c1<c2 && r1<r2){
                for (int c=c2-1;c>c1;c--){
                    res.add(matrix[r2][c]);
                }
                for (int r=r2;r>r1;r--){
                    res.add(matrix[r][c1]);
                }
            }
            r2--;
            r1++;
            c1++;
            c2--;
        }
        int i=0;
        for (int c:res){
            ans[i++]=c;
        }
        return ans;
    }

    public static void main(String[] args) {
        int[][] matrix =new int[][]{
                {1,2,3},{4,5,6},{7,8,9}
        };

        System.out.println(spiralOrder(matrix).toString());
    }
}
